package iscript.uiFramework.components.form 
{
	import flash.display.DisplayObjectContainer;
	import iscript.uiFramework.components.container.ScrollBox;
	import iscript.uiFramework.components.silder.SilderPolicy;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.data.ListBoxModel;
	import iscript.uiFramework.event.ListBoxEvent;
	
	[Event(name="CellRender_Select", type = "iscript.uiFramework.event.ListBoxEvent")]
	[Event(name="CellRender_Open", type = "iscript.uiFramework.event.ListBoxEvent")]
	public class ListBox extends UIComponent
	{
		private var _storeData:Vector.<ListBoxModel>
		private var _listCell:Vector.<ListCellRender>
		private var _box:ScrollBox;
	
		public function ListBox(preferWidth:Number=200,preferHeight:Number=150) 
		{
		   _storeData = new Vector.<ListBoxModel>();
		   _listCell = new Vector.<ListCellRender>();
			super(preferWidth,preferHeight);
		}
		
		override protected function initialize():void 
		{
			_box = new ScrollBox(_preferWidth, _preferHeight)
			_box.HSilderPolicy =  SilderPolicy.OFF;
			_box.render(this);
			
			super.initialize();
		}
		 
		public function get storeData():Vector.<ListBoxModel> 
		{
			return _storeData;
		}
		
		public function set storeData(value:Vector.<ListBoxModel>):void 
		{
		
			if (value == null) value = new Vector.<ListBoxModel>();
			_storeData = value;
			if(IsRender)
			drawListItem();
		}
		
		private function drawListItem():void {
				_box.clear();
			    for (var i:int = 0; i < _storeData.length; i++) 
				{
					 var cell:ListCellRender = new ListCellRender(_storeData[i]);
						 cell.index = i;
						 cell.width = _preferWidth;
						 cell.y = cell.height * i;
						 cell.addEventListener(ListBoxEvent.CellRender_Select, _selectCellRenderLogic)
						 _listCell.push(cell);
					     _box.append(cell);
				}
			}
			
		private function _selectCellRenderLogic(e:ListBoxEvent):void 
		{
			for each (var item:ListCellRender in _listCell) 
			{
				  if (e.target != item) {
					  item.normal();
					  }
			}
			 var event:ListBoxEvent = new ListBoxEvent(ListBoxEvent.CellRender_Select);
			 event.selectData = e.selectData;
			 event.selectIndex = ListCellRender(e.target).index;
			 dispatchEvent(event);
			 trace(event.selectIndex)
		}
	
		override public function set width(value:Number):void 
		{
			_preferWidth = value;
			_box.width = value;
			for each (var item:ListCellRender in _listCell) 
			{
				item.width = value;
			}
			super.width = value;
		}	
		
		override public function get width():Number 
		{
			return _preferWidth;
		}
		
		override public function set height(value:Number):void 
		{
			_preferHeight = value;
			_box.height = value;
			super.height = value;
		}
		
		override public function get height():Number 
		{
			return _preferHeight;
		}
			
		override public function render(container:DisplayObjectContainer = null):void 
		{
			drawListItem();
			super.render(container);
		}
		
	}

}